package org.singledog.dogmail.common.entity;

import lombok.*;

import java.util.Random;

/**
 * 统一返回数据
 *
 * @author ZheMing Liu
 * @since 2022.1.18
 */
@Getter
public class ResponseEntity<T> implements BaseResponse {

    private static final long serialVersionUID = new Random(System.currentTimeMillis()).nextLong();

    private Integer code;

    private String message;

    private Boolean success;

    private T data;

    private ResponseEntity(Integer code, String message, Boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }


    public static ResponseEntity ok() {
        return new ResponseEntity(CommonResponse.OK.getCode(), CommonResponse.OK.getMessage(), true, null);
    }

    public static <T> ResponseEntity<T> ok(T data) {
        return new ResponseEntity(CommonResponse.OK.getCode(), CommonResponse.OK.getMessage(), true, data);
    }

    public static ResponseEntity fail() {
        return new ResponseEntity(CommonResponse.SERVER_ERROR.getCode(), CommonResponse.SERVER_ERROR.getMessage(), false, null);
    }

    public static ResponseEntity fail(Integer code, String message) {
        return new ResponseEntity(code, message, false, null);
    }

    public static ResponseEntity fail(BaseResponse baseResponse) {
        return new ResponseEntity(baseResponse.getCode(), baseResponse.getMessage(), false, null);
    }
}
